package com.autonavi.abs.generator.core;

import com.autonavi.abs.common.exception.MessageException;
import com.autonavi.abs.generator.common.enumeration.TemplateEnum;
import com.autonavi.abs.generator.common.model.ColumnMeta;
import com.autonavi.abs.generator.common.model.TableMeta;
import com.autonavi.abs.generator.core.property.DbProperty;
import com.autonavi.abs.generator.core.db.MetaDataSource;
import com.autonavi.abs.generator.core.generator.Generator;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wwy
 * @version V1.0
 * <p>Description: 资源中心</p>
 * @date 2020-08-17 20:10
 */
@Slf4j
@Getter
@Setter
@Component
public class SourceCenterService {

    @Autowired
    private DbProperty dbProperty;

    /**
     * 数据源
     */
    @Autowired
    private List<MetaDataSource> metaDataSourceList;
    private Map<String, MetaDataSource> metaDataSourceMap = new HashMap<>();
    private MetaDataSource metaDataSource = null;

    /**
     * generator
     */
    @Autowired
    private List<Generator> generatorList;
    private Map<String, Generator> generatorMap = new HashMap<>();

    /**
     * 元数据
     */
    private List<TableMeta> tableMetaList = Lists.newArrayList();
    private Map<String, List<ColumnMeta>> tableColumnMap = Maps.newHashMap();

    @PostConstruct
    public void initMetaData() throws Exception {

        // 初始化数据源
        if (CollectionUtils.isNotEmpty(metaDataSourceList)) {
            metaDataSourceList.stream().forEach((metaDataSource -> {
                if (metaDataSource != null) {
                    metaDataSourceMap.put(metaDataSource.getType(), metaDataSource);
                }
            }));
        }

        if (MapUtils.isEmpty(metaDataSourceMap)) {
            throw new RuntimeException("数据源初始化失败");
        }

        MetaDataSource metaDataSource = getMetaDataSource(dbProperty.getType());
        if (metaDataSource == null) {
            throw new RuntimeException("不支持的数据源类型");
        }
        this.metaDataSource = metaDataSource;

        // 初始化table元数据
        List<TableMeta> tableMetaList = metaDataSource.getTableList();
        if (CollectionUtils.isEmpty(tableMetaList)) {
            throw new RuntimeException("获取表元数据失败");
        }
        this.tableMetaList = tableMetaList;

        // 初始化列元数据
        Map<String, List<ColumnMeta>> tableColumnMap = Maps.newHashMap();
        tableMetaList.stream().forEach((tableMeta -> {
            List<ColumnMeta> columnList = Lists.newArrayList();
            try {
                columnList = metaDataSource.getColumnList(tableMeta.getName());
            } catch (Exception e) {
                throw new MessageException(e.getMessage(), e);
            }
            if (CollectionUtils.isEmpty(columnList)) {
                throw new RuntimeException("获取列元数据失败");
            }

            tableColumnMap.put(tableMeta.getName(), columnList);

        }));
        this.tableColumnMap = tableColumnMap;

    }

    @PostConstruct
    public void initGenerator() {

        if (CollectionUtils.isNotEmpty(generatorList)) {
            generatorList.stream().forEach((generator -> {
                if (generator != null) {
                    generatorMap.put(generator.getTemplateType(), generator);
                }
            }));
        }

        if (MapUtils.isEmpty(generatorMap)) {
            throw new RuntimeException("数据源初始化失败");
        }

    }

    /**
     * 根据db类型获取metaDataSource
     *
     * @param dbType
     * @return
     */
    public MetaDataSource getMetaDataSource(String dbType) {

        if (StringUtils.isBlank(dbType)) {
            throw new RuntimeException("数据源类型不能为空");
        }

        MetaDataSource metaDataSource = metaDataSourceMap.get(dbType.toLowerCase());
        if (metaDataSource == null) {
            throw new RuntimeException("不支持的数据源类型");
        }
        return metaDataSource;
    }

    /**
     * 根据模板类型加载对应generator
     *
     * @param templateType
     * @return
     */
    public Generator getGenerator(TemplateEnum templateType) {

        if (templateType == null) {
            throw new RuntimeException("模板类型不能为空");
        }

        Generator generator = generatorMap.get(templateType.name());
        if (generator == null) {
            throw new RuntimeException("不支持的生成器");
        }
        return generator;
    }

    /**
     * 获取generator map
     *
     * @return
     */
    public List<Generator> getGeneratorList() {
        return generatorList;
    }

    public List<ColumnMeta> getColumnMetaList(String tableName) {
        return tableColumnMap.get(tableName);
    }
}
